package com.glc.hms.system.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glc.hms.common.exception.ServiceException;
import com.glc.hms.core.common.ConstantFactory;
import com.glc.hms.core.shiro.ShiroUser;
import com.glc.hms.core.shiro.ShiroUtil;
import com.glc.hms.system.dao.SysDeptMapper;
import com.glc.hms.system.dao.SysUserMapper;
import com.glc.hms.system.entity.SysDept;
import com.glc.hms.system.entity.SysUser;
import com.glc.hms.system.model.param.AddDeptParam;
import com.glc.hms.system.model.vo.DeptTreeVO;
import com.glc.hms.system.service.ISysDeptService;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门管理 服务实现类
 * </p>
 *
 * @author Santa
 * @since 2019-11-20
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询用户的部门
     * @return
     */
    @Override
    public List<DeptTreeVO> queryDeptByUserDeptId(){
        ShiroUser user = ShiroUtil.getUserNotNull();
        Long deptId = user.getDeptId();
        List<DeptTreeVO> deptTreeVOS = this.queryChildDept(deptId);
        List<DeptTreeVO> res = this.queryParentDept(deptId, deptTreeVOS);
        return res;
    }


    /**
     * 添加部门
     * @param param
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDept(AddDeptParam param){
        SysDept sysDept = new SysDept();
        sysDept.setName(param.getNewDeptName());
        sysDept.setParentId(param.getParentId());
        sysDept.setOrderNum(param.getOrderNum());
        deptMapper.insert(sysDept);
    }


    /**
     * 修改部门
     * @param param
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDept(SysDept param){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", param.getDeptId());
        SysDept sysDept = deptMapper.selectOne(queryWrapper);
        sysDept.setName(param.getName());
        sysDept.setOrderNum(param.getOrderNum());
        this.updateById(sysDept);
    }


    /**
     * 删除部门
     * @param param
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delDept(Long deptId){
        //判断该部门及其子部门是否有人  有人则无法删除
        List<Long> longs = ConstantFactory.me().queryChileDeptId(deptId);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_id",longs);
        List<SysUser> sysUsers = userMapper.selectList(queryWrapper);
        if (sysUsers.size()>0) {
            throw new ServiceException(500,"该组织下存在用户,不允许删除!");
        }else{
            deptMapper.deleteById(deptId);
        }
    }

    //递归查询该部门的下级部门
    private List<DeptTreeVO> queryChildDept(Long deptId){
        List<DeptTreeVO> list = Lists.newArrayList();
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", deptId);
        queryWrapper.orderByAsc("order_num");
        List<SysDept> sysDepts = deptMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(sysDepts)){
            for (SysDept sysDept : sysDepts) {
                DeptTreeVO deptTreeVO = new DeptTreeVO();
                BeanUtils.copyProperties(sysDept,deptTreeVO);
                //递归子集
                List<DeptTreeVO> deptTreeVOS = queryChildDept(sysDept.getDeptId());
                deptTreeVO.setChildren(deptTreeVOS);
                list.add(deptTreeVO);
            }
        }
        return list;
    }

    // 递归查询父级
    private List<DeptTreeVO> queryParentDept(Long deptId,List<DeptTreeVO> child){
        List<DeptTreeVO> list = Lists.newArrayList();
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", deptId);
        SysDept sysDept = deptMapper.selectOne(queryWrapper);
        DeptTreeVO deptTreeVO = new DeptTreeVO();
        BeanUtils.copyProperties(sysDept,deptTreeVO);
        deptTreeVO.setChildren(child);
        list.add(deptTreeVO);
        if(sysDept.getParentId() != 0){
            list = queryParentDept(sysDept.getParentId(), list);
        }
        return list;
    }
}
